package leetcode

import kotlinetc.println

fun main(args: Array<String>) {


    plusOne(intArrayOf(9)).asList().println()
}


fun plusOne(digits: IntArray): IntArray {

    //默认加一，默认是进位
    var carry = true

    for (i in digits.size - 1 downTo 0) {

        if (carry) {
            if (digits[i] == 9) {
                digits[i] = 0
                carry = true
            } else {
                digits[i]++
                carry = false
            }

        } else {
            break
        }
    }

    return if (carry) {
        val array = IntArray(digits.size + 1, { it ->
            0
        })
        array[0] = 1
        array
    } else digits
}